﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using CommonsLib.Settings;
using System.Data;

using System.Security.Cryptography;
using MaxLeaf.Platform.Functions;
using MaxLeaf.Platform.Commons;

namespace MaxLeaf.Platform.DALImplementation
{
    public class poOrderRecvDtlDAL :BaseDAL 
    {
        public override string IgnoreUpdateFields
        {
            get
            {
                return "RecvQty";
            }
        }

        public void SetPosted(DBAccess dbcon, int intKey, int intOrderHdrId, int intOrderDtlId)
        {
            string strSql = @"
                update poOrderRecvDtl set  RecvQty=RecvQty+Qty,Qty=0  where DtlID=@FInterID  
                ;update poOrderRecvDtl set  posted=1  where DtlID=@FInterID and posted=0 and RecvQty>=ReqQty";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intKey.ToString())
 
            });
        }

        public List<int> GetUnpostKeys(DBAccess dbcon, int intHdrID)
        {
            List<int> lst = new List<int>();
            string strSql = "select DtlID from poOrderRecvDtl where HdrID=@HdrID and posted=0 and is_delete=0 ";
            using (DbDataReader rdr = dbcon.GetDataReader(strSql, new SqlParameter[] { new SqlParameter("@HdrID", intHdrID.ToString())
            }))
            {
                while (rdr.Read())
                {
                    lst.Add(rdr.GetInt32(0));
                }
                
            }

            return lst;
        }

        public void SetUnPosted(DBAccess dbcon, int intRecvHdrId, int intRecvDtlId, double dblRecvQty)
        {
            string strSql = @"
                update poOrderRecvDtl set  posted=0,RecvQty=RecvQty-@Qty  where DtlID=@FInterID  
                ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intRecvDtlId.ToString()),new SqlParameter("@Qty", dblRecvQty.ToString())
 
            });
        }
    }
}
